package com.example.beidouapplication;

import static com.example.beidouapplication.Menu.FixFormat;
import static com.example.beidouapplication.Menu.FixGaussPlane;
import static com.example.beidouapplication.Menu.I_C_CONF_S1;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import com.example.beidouapplication.databinding.FragmentShuruxinzuobiaoBinding;
import com.example.beidouapplication.databinding.FragmentShuruxinzuobiaoV2Binding;
import com.example.beidouapplication.viewmodel.Key;
import com.example.beidouapplication.viewmodel.KeyEvent;
import com.jay.easykeyboard.SystemKeyBoardEditText;
import com.jay.easykeyboard.action.KeyBoardActionListener;


public class FragmentShuRuZuoBiaoV2 extends Fragment implements KeyBoardActionListener {

    Button btn_queren;
    SystemKeyBoardEditText input1;
    SystemKeyBoardEditText input1_decial;
    SystemKeyBoardEditText input2;
    SystemKeyBoardEditText input2_decial;

    SystemKeyBoardEditText input3;
    SystemKeyBoardEditText input3_decial;

    static FragmentShuRuZuoBiaoV2 instance = new FragmentShuRuZuoBiaoV2();

    FragmentShuruxinzuobiaoV2Binding binding;


    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        int layout = R.layout.fragment_shuruxinzuobiao_v2;

        binding = DataBindingUtil.inflate(inflater, layout, container, false);
        binding.setZuobiao(MainActivity.cszb);


        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        btn_queren = (Button) getActivity().findViewById(R.id.btn_shuruzuobiao_queren_v2);
        input1 = getActivity().findViewById(R.id.edit_srzb_v2_1);
        input2 = getActivity().findViewById(R.id.edit_srzb_v2_2);
        input3 = getActivity().findViewById(R.id.edit_srzb_v2_3);
        input1_decial= getActivity().findViewById(R.id.edit_srzb_v2_1_decimal);
        input2_decial= getActivity().findViewById(R.id.edit_srzb_v2_2_decimal);
        input3_decial= getActivity().findViewById(R.id.edit_srzb_v2_3_decimal);

        View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText editText = (EditText) v;
                String setVal = ((EditText) v).getText().toString();
                ObservableField<String> observableField = null;
                if (v == input1) {
                    observableField = MainActivity.cszb.input1;
                } else if (v == input2) {
                    observableField = MainActivity.cszb.input2;
                } else if (v == input3) {
                    observableField = MainActivity.cszb.input3;
                }

                if (hasFocus && "0".equals(editText.getText().toString())) {
//                    // 当EditText获得焦点，并且其值为"0"时，清空EditText
//                    setVal = "";
//                    editText.setText(setVal);
//                    // 可选：设置光标位置到EditText的开始
//                    editText.setSelection(0);
                } else if (!hasFocus && TextUtils.isEmpty(editText.getText().toString())) {
                    setVal = "0";
                    editText.setText(setVal);
                }

                if (observableField != null) {
                    observableField.set(setVal);

                }
            }
        };


        TextView.OnEditorActionListener editorActionListener = new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, android.view.KeyEvent event) {
//                if (event != null && event.getKeyCode() == android.view.KeyEvent.KEYCODE_ENTER) {
                if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                    EditText editText = (EditText) v;

                    if (TextUtils.isEmpty(editText.getText())) {
//                        editText.setText("0");
                    }
//                    return true;
                }
                return false;
//                return super.onKeyDown(keyCode, event);
            }
        };
//        input1.setOnEditorActionListener(editorActionListener);
//        input1.setOnFocusChangeListener(focusChangeListener);
//
//        input2.setOnEditorActionListener(editorActionListener);
//        input2.setOnFocusChangeListener(focusChangeListener);
//
//        input3.setOnEditorActionListener(editorActionListener);
//        input3.setOnFocusChangeListener(focusChangeListener);

        input1.setOnKeyboardActionListener(new NextTextWatcher(input2));
        input2.setOnKeyboardActionListener(new NextTextWatcher(input3));
        input3.setOnKeyboardActionListener(this);
        input1_decial.setOnKeyboardActionListener(new NextTextWatcher(input2_decial));
        input2_decial.setOnKeyboardActionListener(new NextTextWatcher(input3_decial));
        input3_decial.setOnKeyboardActionListener(this);



        btn_queren.setOnClickListener(v -> {
            
            input1.clearFocus();
            input2.clearFocus();
            input3.clearFocus();

            if (Menu.stage == I_C_CONF_S1) {
                Menu.keyQueue.add(new KeyEvent(Key.ENTER, -1));
                return;
            }

            try {
                ObservableField<String> input = MainActivity.cszb.input1;
                String trim = input.get().trim();
                if (trim.length() == 0) {
                    trim = "0";
                    input.set(trim);
                }
                Double.parseDouble(trim);
            } catch (NumberFormatException e) {
                input1.setError("请输入合法数字");
                input1.getText().clear();
                input1.setHint("请输入合法数字");
                return;
            }
            try {
                ObservableField<String> input = MainActivity.cszb.input2;
                String trim = input.get().trim();
                if (trim.length() == 0) {
                    trim = "0";
                    input.set(trim);
                }
                Double.parseDouble(trim);
            } catch (NumberFormatException e) {

                input2.setError("请输入合法数字");
                input2.getText().clear();
                input2.setHint("请输入合法数字");
                return;

            }
            try {
                ObservableField<String> input = MainActivity.cszb.input3;
                String trim = input.get().trim();
                if (trim.length() == 0) {
                    trim = "0";
                    input.set(trim);
                }
                Double.parseDouble(trim);
            } catch (NumberFormatException e) {

                input3.setError("请输入合法数字");
                input3.getText().clear();
                input3.setHint("请输入合法数字");
                return;

            }

            Menu.keyQueue.add(new KeyEvent(Key.ENTER, -1));
        });

        ;
    }

    @Override
    public void onResume() {
        super.onResume();
        input1.setFocusable(true);
        input2.setFocusable(true);
        input3.setFocusable(true);
        input1_decial.setFocusable(true);
        input2_decial.setFocusable(true);
        input3_decial.setFocusable(true);
        if (FixFormat == FixGaussPlane) {
            input1.setVisibility(View.VISIBLE);
            input2.setVisibility(View.VISIBLE);
            input3.setVisibility(View.VISIBLE);

            input1_decial.setVisibility(View.GONE);
            input2_decial.setVisibility(View.GONE);
            input3_decial.setVisibility(View.GONE);
            input1.setInputType(InputType.TYPE_CLASS_NUMBER);
            input2.setInputType(InputType.TYPE_CLASS_NUMBER);
            input3.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else {
            input1_decial.setVisibility(View.VISIBLE);
            input2_decial.setVisibility(View.VISIBLE);
            input3_decial.setVisibility(View.VISIBLE);

            input1.setVisibility(View.GONE);
            input2.setVisibility(View.GONE);
            input3.setVisibility(View.GONE);


            input1_decial.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
            input2_decial.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL| InputType.TYPE_NUMBER_FLAG_SIGNED);
            input3_decial.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL| InputType.TYPE_NUMBER_FLAG_SIGNED);


        }


    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onTextChange(Editable editable) {

    }

    @Override
    public void onClear() {

    }

    @Override
    public void onClearAll() {

    }
}

class NextTextWatcher implements KeyBoardActionListener{

    SystemKeyBoardEditText next;
    NextTextWatcher(SystemKeyBoardEditText next) {
       this.next = next;
    }

    @Override
    public void onComplete() {
        next.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0,0,0));
        next.dispatchTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, 0,0,0));
    }

    @Override
    public void onTextChange(Editable editable) {

    }

    @Override
    public void onClear() {

    }

    @Override
    public void onClearAll() {

    }
}

class EmptyKeyBoardActionListener implements KeyBoardActionListener{


    @Override
    public void onComplete() {
    }

    @Override
    public void onTextChange(Editable editable) {

    }

    @Override
    public void onClear() {

    }

    @Override
    public void onClearAll() {

    }
}